package ee.maoinvest.frontend.controller.command.electricitymetercommands;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import ee.maoinvest.backend.model.data.ElectricityMeter;
import ee.maoinvest.backend.services.ElectricityMeterService;
import ee.maoinvest.backend.services.implementations.ElectricityMeterServiceImpl;
import ee.maoinvest.frontend.controller.command.Command;
import ee.maoinvest.frontend.controller.formstringdataconverters.electricitymeterforms.DeleteElectricityMeterFormConverter;

public class FillDeleteElectricityMeterPageFormConverterWithInitialData implements Command
{
	private Logger logger = Logger.getLogger(FillDeleteElectricityMeterPageFormConverterWithInitialData.class);
	private int operationResult = 0;
	private final int OPERATION_SUCCESSFUL = 1;
	private final int PROBABLY_A_DATABASE_ERROR = -1;
	
	@Override
	public int executeCommand(HttpServletRequest request,
			HttpServletResponse response) 
	{
		DeleteElectricityMeterFormConverter deleteElectricityMeterFormConverter = null;
		List<ElectricityMeter> listOfAllElectricityMetersThatCanBeDeleted = null;
		ElectricityMeterService electricityMeterService = null;
		
		try 
		{
			electricityMeterService = new ElectricityMeterServiceImpl();
			deleteElectricityMeterFormConverter = new DeleteElectricityMeterFormConverter();
			
			//Need arvestid ja koefitsendid ei tohi olla maha kirjutatud. Lisaks ei tohi arvesti olla kasutuses notInUSe = false!!!!
			listOfAllElectricityMetersThatCanBeDeleted = electricityMeterService.getAllElectricityMetersThatHaveNoChildMetersAndAreNotBoundedWithContractElseThanDefaultContract();
			
			deleteElectricityMeterFormConverter.fillWithInitialData(listOfAllElectricityMetersThatCanBeDeleted);
			request.setAttribute("deleteElectricityMeterFormConverter", deleteElectricityMeterFormConverter);
			
			HttpSession session = request.getSession();
			session.setAttribute("listOfAllElectricityMetersThatCanBeDeleted", listOfAllElectricityMetersThatCanBeDeleted);
			operationResult = OPERATION_SUCCESSFUL;
			
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
			return PROBABLY_A_DATABASE_ERROR;
		}
		
		return operationResult;
	}
}
